package com.moon.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.moon.R;
import com.moon.constants.MoonConstants;
import com.moon.model.Reply;
import com.moon.utils.BitmapImageCache;
import com.moon.utils.DateUtils;

/**
 * @author paul
 * @date 2014年5月17日
 */
public class ReplyListAdapter extends ArrayAdapter<Reply> {

    private LayoutInflater inflater;
    public ImageLoader     imageLoader;

    public ReplyListAdapter(Context context, List<Reply> list) {
        super(context, 0, list);
        inflater = LayoutInflater.from(context);
       // imageLoader= new ImageLoader(context);
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(requestQueue, BitmapImageCache.getInstance());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Reply reply = getItem(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.reply_item_adapter, null);
            viewHolder.userImage = (ImageView) convertView.findViewById(R.id.user_image);
            viewHolder.userName = (TextView) convertView.findViewById(R.id.user_name);
            viewHolder.replyContent = (TextView) convertView.findViewById(R.id.reply_content);
            viewHolder.replyTime = (TextView) convertView.findViewById(R.id.reply_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ImageListener imageListener = ImageLoader.getImageListener(viewHolder.userImage,
                R.drawable.app_panel_pic_icon, R.drawable.app_panel_pic_icon);
        imageLoader.get(MoonConstants.MOON_DOMAIN_STATIC_HEAD  + reply.getImgUrl(),
                imageListener, 0, 0);
       // imageLoader.DisplayImage(MoonConstants.MOON_DOMAIN_STATIC_HEAD  + reply.getImgUrl(), viewHolder.userImage);
        viewHolder.userName.setText(reply.getUserName());
        viewHolder.replyContent.setText(reply.getContent());
        viewHolder.replyTime.setText(DateUtils.formatDate2String(reply.getGmtCreate(),
                DateUtils.DATEPATTERN_TIME));

        //        TextView name = (TextView) convertView.findViewById(R.id.item_name);
        //        name.setText(info.getName());

        return convertView;
    }

    class ViewHolder {
        ImageView userImage;
        TextView  userName;
        TextView  replyContent;
        TextView  replyTime;
    }

   
    @Override
    public Reply getItem(int position) {
        return super.getItem(position);
    } 
    
}
